package com.ruoyi.gp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gp.domain.bo.ShoppingPlatformBo;
import com.ruoyi.gp.domain.vo.ShoppingPlatformVo;
import com.ruoyi.gp.domain.ShoppingPlatform;
import com.ruoyi.gp.mapper.ShoppingPlatformMapper;
import com.ruoyi.gp.service.IShoppingPlatformService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 购物平台Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-07
 */
@RequiredArgsConstructor
@Service
public class ShoppingPlatformServiceImpl implements IShoppingPlatformService {

    private final ShoppingPlatformMapper baseMapper;

    /**
     * 查询购物平台
     */
    @Override
    public ShoppingPlatformVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询购物平台列表
     */
    @Override
    public TableDataInfo<ShoppingPlatformVo> queryPageList(ShoppingPlatformBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ShoppingPlatform> lqw = buildQueryWrapper(bo);
        Page<ShoppingPlatformVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询购物平台列表
     */
    @Override
    public List<ShoppingPlatformVo> queryList(ShoppingPlatformBo bo) {
        LambdaQueryWrapper<ShoppingPlatform> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ShoppingPlatform> buildQueryWrapper(ShoppingPlatformBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ShoppingPlatform> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getShoppingPlatformName()), ShoppingPlatform::getShoppingPlatformName, bo.getShoppingPlatformName());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), ShoppingPlatform::getUrl, bo.getUrl());
        lqw.eq(bo.getDoctorId() != null, ShoppingPlatform::getDoctorId, bo.getDoctorId());
        return lqw;
    }

    /**
     * 新增购物平台
     */
    @Override
    public Boolean insertByBo(ShoppingPlatformBo bo) {
        ShoppingPlatform add = BeanUtil.toBean(bo, ShoppingPlatform.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改购物平台
     */
    @Override
    public Boolean updateByBo(ShoppingPlatformBo bo) {
        ShoppingPlatform update = BeanUtil.toBean(bo, ShoppingPlatform.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ShoppingPlatform entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除购物平台
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

}
